package com.yanyu.space.common.sleuth.jdbc;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.sleuth.instrument.jdbc.P6SpyContextJdbcEventListenerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

import java.util.List;

//@ConditionalOnClass(P6DataSource.class)
@ConditionalOnProperty(name = "spring.sleuth.jdbc.p6spy.enabled", havingValue = "true", matchIfMissing = true)
public class JdbcAutoConfig {

//    @Bean
//    @ConditionalOnMissingBean
//    JdbcEventListenerFactory traceJdbcEventListenerFactory(ObjectProvider<List<JdbcEventListener>> listeners) {
//        JdbcEventListenerFactory jdbcEventListenerFactory = new DefaultJdbcEventListenerFactory();
//        List<JdbcEventListener> listenerList = listeners.getIfAvailable();
//        return listenerList != null ? new P6SpyContextJdbcEventListenerFactory(jdbcEventListenerFactory, listenerList)
//                : jdbcEventListenerFactory;
//    }

    class P6SpyContextJdbcEventListenerFactory1 implements BeanPostProcessor {
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            if(bean instanceof P6SpyContextJdbcEventListenerFactory) {
                System.out.print("----------");
//                ((P6SpyContextJdbcEventListenerFactory) bean).createJdbcEventListener();
            }
            return bean;
        }
    }
}
